package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.Setmeal;
import com.itheima.service.MemberService;
import com.itheima.service.OrderService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/orderController")
public class Order4JavaController {
    @Reference
    private OrderService orderService;
    @Reference
    private MemberService memberService;

    /**
     * 后管预约管理-预约查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
           PageResult result = orderService.findPage(queryPageBean);
           return result;
        }


    /**
     * 后管预约管理-添加
     * @param order
     * @param setmealIds
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Order order , Integer[] setmealIds){
        try {
            Result result = orderService.add(order, setmealIds);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    /**
     * 后管预约管理-删除预约
     * @param id
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            orderService.delete(id);
            return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
        }
    }

    /**
     * 后管预约管理-编辑数据回显
     * @param orderId
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer orderId){
        try {
            Map map = orderService.findById(orderId);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    /**
     * 后管预约管理-通过日期查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByDate")
    public PageResult findByDate(@RequestBody QueryPageBean queryPageBean) {
        PageResult result = orderService.findByDate(queryPageBean);
        return result;
    }

    /**
     * 后管预约管理-通过套餐类型查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByOrderType")
    public PageResult findByOrderType(@RequestBody QueryPageBean queryPageBean) {
        PageResult result = orderService.findByOrderType(queryPageBean);
        return result;
    }

    /**
     * 后管预约管理-通过预约状态查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByOrderStatus")
    public PageResult findByOrderStatus(@RequestBody QueryPageBean queryPageBean) {
        PageResult result = orderService.findByOrderStatus(queryPageBean);
        return result;
    }

    /**
     * 后管预约管理-通过手机号或用户姓名查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByOrderString")
    public PageResult findByOrderString(@RequestBody QueryPageBean queryPageBean) {
        PageResult result = orderService.findByOrderString(queryPageBean);
        return result;
    }

    /**
     * 后管预约管理-电话预约添加(会员添加)
     * @return
     */
    @RequestMapping("/add4Order")
    public Result add4Order(@RequestBody Member member){
        try {
            Integer memberId = memberService.add4Order(member);
            return new Result(true,MessageConstant.ADD_MEMBER_SUCCESS,memberId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MEMBER_FAIL);
        }
    }

    /**
     * 后管预约管理-编辑(套餐数据回显)
     * @param orderId
     * @return
     */
    @RequestMapping("/findSetmealIdsByOrderId")
    public Result findSetmealIdsByOrderId(Integer orderId){
        try {
            List<Integer> setmealIds = orderService.findSetmealIdsByOrderId(orderId);
            return new Result(true,MessageConstant.QUERY_SETMEALLIST_SUCCESS,setmealIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEALLIST_FAIL);
        }
    }

    /**
     * 后管预约管理-编辑(写入数据)
     * @param order
     * @param setmealId
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Order order,Integer[] setmealId){
        try {
            Result result = orderService.edit(order,setmealId);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }

    /**
     * 后管预约管理-按钮状态改变
     * @param id
     * @return
     */
    @RequestMapping("/changeOrderStatus")
    public Result changeOrderStatus(Integer id){
        try {
            orderService.changeOrderStatus(id);
            return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }
}
